<?php

namespace app\admin\controller;

use think\Controller;

class Admin extends Controller
{
    protected $appPath = '../application';

    public function initialize()
    {
        if (!is_login()) {
            if (Request()->isAjax()) {
                $this->error('请登录', 'admin/user/login');
            } else {
                $this->redirect('admin/user/login');
            }
        }
        error_reporting(E_ALL & ~E_NOTICE);

        $this->_dealMenu();
        if (!is_administrator()) {
            $this->_checkAuth();
        }
        $this->setBreadcrumb();


        parent::initialize();
    }


    protected function _checkAuth()
    {
        $sessionauth = session('user_auth.auth');
        $myauth = $sessionauth ? explode(',', $sessionauth) : [];

        $class = strtolower(request()->module() . '/' . request()->controller() . '/' . request()->action());
        if (!in_array($class, $myauth)) {
            $this->error('没有权限', url('admin/index/index'));
        }


    }

    /**
     * 菜单权限处理
     * @return array
     *  駿濤
     */
    protected function _dealMenu()
    {
        $slide = $this->_leftSlide();
        $sessionauth = session('user_auth.auth');
        $myauth = $sessionauth ? explode(',', $sessionauth) : [];
        if (empty($myauth)) {
            $slide = [];
        } else {
            foreach ($slide as $k => $v) {
                if (!empty($v['child'])) {
                    foreach ($v['child'] as $kk => $vv) {
                        if (!in_array($vv['menu'], $myauth)) {
                            unset($v['child'][$kk]);
                        }
                    }
                }
                if (empty($v['child']) && empty($v['menu'])) {
                    unset($slide[$k]);
                } elseif (!empty($v['menu'])) {
                    if (!in_array($v['menu'], $myauth)) {
                        unset($slide[$k]);
                    }
                }
            }
        }
        $slide = array_values($slide);

        $this->assign('left_slide', $slide);
        return $slide;
    }

    protected function _leftSlide()
    {
        $slide = [];
        return $slide;
    }


    public function setStatus()
    {
        $table = input('table', '', 'text');
        $id = input('id', '', 'text');
        $status = input('status', 1, 'intval');
        if (empty($id) || empty($table)) {
            $this->error('修改失败');
        }
        $key = db($table)->getPk(); //获取主键
        db($table)->where([$key => ['in', $id]])->update(['status' => $status]);
        if ($status == -1) {
            $this->success('删除成功', '');
        } else {
            $this->success('修改成功', '');
        }
    }


    /**
     * 设置面包屑
     * @param array $breadcrumb
     *  駿濤
     */
    protected function setBreadcrumb($breadcrumb = [])
    {
        $this->assign('breadcrumb', $breadcrumb);
    }

    protected function _setSlide($slide = '')
    {
        $this->assign('slide_active', $slide ?: '');
    }


}
